<template>
  <div ref="mapContainer"></div>
</template>

<script>
export default {
  mounted() {
    const initMap = () => {
      var map = new BMap.Map(this.$refs.mapContainer);
      this.map = map;
      map.enableScrollWheelZoom(true);
      this.$emit('init', this);
    }

    // 加载百度地图api js
    window.BMAP_PROTOCOL = 'https';
    window.BMap_loadScriptTime = new Date().getTime();
    let script = document.createElement('script');
    script.src = 'https://api.map.baidu.com/getscript?s=1&v=3.0&ak=eG3uGEnyuW1igyG7C9PemZ4LPV6pNCBc&services=&t='
      + window.BMap_loadScriptTime;
    script.onload = () => {
      initMap();
    };
    script.onerror = () => {
      this.$message.error('发生错误，请重试');
    };
    this.$refs.mapContainer.appendChild(script);
  }
}
</script>

